<?php
declare(strict_types=1);

namespace App\ZxAdmin\Sys;

use App\Controller;
use Field\SysDictConst;
use Field\SysDictColumns;
use Field\SysDictRow;

// hook controller_zx_admin_sys_sys_dict_use.php

/**
 * @title   字典管理
 * @auth    true
 * @login   true
 * @menu    true
 * @component   vlist/index.vue
 * @icon    el-icon-document-copy
 * @active  true
 * @affix   false
 * @rank    1
 */
class SysDict extends Controller
{
    // hook controller_zx_admin_sys_sys_dict_public_start.php



    // hook controller_zx_admin_sys_sys_dict_public_end.php

    /**
     * @title  下发配置
     * @rank    0
     */
    public function action_Conf_PUT()
    {
        // hook controller_zx_admin_sys_sys_dict_conf_start.php
        $conf=['pageSize'=>0,'remoteSort'=>0,'remoteFilter'=>0,'remoteSummary'=>0,'hidePagination'=>1,'hideRefresh'=>1,'hideDo'=>0,'add'=>1,'start'=>0,'stop'=>0,'delete'=>1,'import'=>0,'export'=>0,'pageSizes'=>[10,20,25,50,100,500,1000,10000],'TreeData'=>0,'enable'=>1,'disable'=>1,'chartRange'=>0,'copy'=>1];
        $Columns = [['table'=>'SysDict','prop'=>'DictKey','label'=>'字典Key','hidden'=>0,'search'=>0,'add'=>1,'edit'=>0,'field'=>1,'sortable'=>0,'batch'=>0,'type'=>'input','editable'=>1,'width'=>'200'],['table'=>'SysDict','prop'=>'DictName','label'=>'字典名字','hidden'=>0,'search'=>0,'add'=>1,'edit'=>0,'field'=>1,'sortable'=>0,'batch'=>0,'type'=>'input','editable'=>1,'width'=>'200'],['table'=>'SysDict','prop'=>'Content','label'=>'字典配置','hidden'=>0,'search'=>0,'add'=>0,'edit'=>0,'field'=>1,'sortable'=>0,'batch'=>0,'type'=>'json','editable'=>1,'width'=>500],['table'=>'SysDict','prop'=>'Status','label'=>'状态','hidden'=>0,'search'=>0,'add'=>0,'edit'=>0,'field'=>1,'sortable'=>0,'batch'=>1,'type'=>'dict','dict'=>'Status','editable'=>1,'width'=>80]];
        $Filter = [];
        $Tools = [];

        // hook controller_zx_admin_sys_sys_dict_conf_end.php
        $this->response('0000', ['Conf' =>$conf, 'Filter' => $Filter, 'Tools' =>$Tools, 'Columns' => $Columns]);
    }

    /**
     * @title   下发列表数据
     * @rank    73
     */
    public function action_Index_GET()
    {
        // hook controller_zx_admin_sys_sys_dict_index_start.php
        $page = $this->request->param('page', 1);
        $size = $this->request->param('limit', 10000);
        $field = $this->request->param('field', 'DictID');
        $sort = $this->request->param('sort', -1);
        $order = [];
        !empty($field) && $order[$field] = $sort == 'asc' ? -1 : 1;
        $cond = [];
        
        $data = $this->SysDict->GetList($cond, $order, $page, $size);
        
        //foreach ($data['data'] as &$row) {
            
            //unset($row['PassWord'],$row['Salt']);
       // }

        // hook controller_zx_admin_sys_sys_dict_index_end.php
        $this->response('0000', $data);
    }
    
    /**
     * @title   新增配置
     * @rank    72
     */
    public function action_Add_PUT()
    {
        // hook controller_zx_admin_sys_sys_dict_add_put_start.php
        $AddForm = [
            ['table'=>'SysDict','prop'=>'DictKey','label'=>'字典Key','hidden'=>0,'search'=>0,'add'=>1,'edit'=>0,'field'=>0,'sortable'=>0,'component'=>'input',
                'rules'=> [
                     ['required'=>true,'message'=>'请输入字典名'],
                     ['pattern'=>'^[A-Za-z]+$','message'=>'仅限输入字母']
        ],],['table'=>'SysDict','prop'=>'DictName','label'=>'字典名字','hidden'=>0,'search'=>0,'add'=>1,'edit'=>0,'field'=>0,'sortable'=>0,'component'=>'input']];
        // hook controller_zx_admin_sys_sys_dict_add_put_end.php
        $this->response('0000', ['data' => $AddForm]);
    }

    /**
     * @title   新增提交
     * @rank    71
     */
    public function action_Add_POST()
    {
        // hook controller_zx_admin_sys_sys_dict_add_post_start.php
        $data = [];
        $data['DictKey']=$this->request->param('DictKey',);
		$data['DictName']=$this->request->param('DictName',);
        $r = $this->SysDict->insert($data);
        // make addformpost SysDict ZxAdmin\Sys
        // hook controller_zx_admin_sys_sys_dict_add_post_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r],$r?'操作成功':'操作失败');
    }
    
    
    
    /**
     * @title   编辑字段
     * @rank    60
     */
    public function action_Field_POST()
    {
        // hook controller_zx_admin_sys_sys_dict_field_post_start.php
        $cond = [];
        $cond['DictID']=$this->request->param('DictID',0);
        $field = $this->request->param('field', '');
        $value = $this->request->param('value', '');
        $this->CheckEmpty([$field,...$cond], ['待修改数据','待修改字段']);
        $field_fields = ['DictKey','DictName','Content','Status'];
        $tmp_field=rtrim($field,'+-*/=');
        !in_array($tmp_field, $field_fields, true) && $this->response('0001', [], '字段不允许修改');
        !in_array($tmp_field, SysDictConst::fields, true) && $this->response('0001', [], '字段不允许修改');
        $update = [];
        switch ($tmp_field) {
        case 'Status':
             $update[$field] = intval($value);
            break;
         case 'DictKey':
         case 'DictName':
         case 'Content':
             $update[$field] = $value;
            break;
        }
        $r = !empty($update) ? $this->SysDict->update($cond, $update) : false;
        // hook controller_zx_admin_sys_sys_dict_field_post_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r],$r?'操作成功':'操作失败');
    }
    
    
    /**
     * @title   批量编辑字段
     * @rank    60
     */
    public function action_Update_POST()
    {
        // hook controller_zx_admin_sys_sys_dict_update_post_start.php
        $data = $this->request->param('data', []);
        $cond=[];
        foreach ($data as $row){
            isset($row['DictID']) && $cond['DictID'][]=intval($row['DictID']);
        }
        $field = $this->request->param('field', '');
        $value = $this->request->param('value', '');
        $this->CheckEmpty([$field,...$cond], ['待修改数据','字段']);
        $update_fields = ['Status'];
        $tmp_field=rtrim($field,'+-*/=');
        !in_array($tmp_field, $update_fields, true) && $this->response('0001', [], '字段不允许修改');
        !in_array($tmp_field, SysDictConst::fields, true) && $this->response('0001', [], '字段不允许修改');
        $update = [];
        switch ($tmp_field) {
        case 'Status':
             $update[$field] = intval($value);
            break;
        }
        $r = !empty($update) ? $this->SysDict->update($cond, $update) : false;
        // hook controller_zx_admin_sys_sys_dict_update_post_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r],$r?'操作成功':'操作失败');
    }
    

    
    /**
     * @title   启用
     * @rank    41
     */
    public function action_Enable_POST()
    {

        // hook controller_zx_admin_sys_sys_dict_enable_start.php
        $data = $this->request->param('data', []);
        $cond=[];
        foreach ($data as $row){
                isset($row['DictID']) && $cond['DictID'][]=intval($row['DictID']);
        }
        $this->CheckEmpty($cond, ['待修改数据']);
        $r = $this->SysDict->update($cond, [SysDictConst::Status => 1]);
        // hook controller_zx_admin_sys_sys_dict_enable_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r],$r?'操作成功':'操作失败');
    }

    /**
     * @title   禁用
     * @rank    40
     */
    public function action_Disable_POST()
    {
        // hook controller_zx_admin_sys_sys_dict_disable_start.php
        $data = $this->request->param('data', []);
        $cond=[];
        foreach ($data as $row){
                isset($row['DictID']) && $cond['DictID'][]=intval($row['DictID']);
        }
        $this->CheckEmpty($cond, ['操作数据为空']);
        $r = $this->SysDict->update($cond, [SysDictConst::Status => 0]);
        // hook controller_zx_admin_sys_sys_dict_disable_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r],$r?'操作成功':'操作失败');
    }
    
    
    /**
     * @title   下拉选择属性
     * @rank    30
     */
    public function action_Option_GET()
    {
        // hook controller_zx_admin_sys_sys_dict_option_start.php

        $value = $this->request->param('value');
        $cond = [
            'OptionField'=>['LIKE'=>$value]
        ];
        $order = [];
        $list = $this->SysDict->select($cond, $order, 1, 100);
        $data=[];
        foreach ($list as $row){
            $data[]=[
              'label'=>$row['OptionLabel'],
              'value'=>$row['OptionValue'],
            ];
        }
        // hook controller_zx_admin_sys_sys_dict_option_end.php
        $this->response('0000', ['data' => $data]);
    }
    
    
    

      
     /**
      * @title   删除数据
      * @rank    50
      */
    public function action_Index_Delete()
    {
        // hook controller_zx_admin_sys_sys_dict_delete_start.php
        $data = $this->request->param('data', []);
        $cond=[];
        foreach ($data as $row){
            isset($row['DictID']) && $cond['DictID'][]=intval($row['DictID']);
        }
        $this->CheckEmpty($cond, ['操作数据为空']);
        $r = $this->SysDict->delete($cond);
        // hook controller_zx_admin_sys_sys_dict_delete_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r]);
    }
    
    // hook controller_zx_admin_sys_sys_dict_end.php
}

?>